import axios from 'axios'
import router from '../router'
// if (process.env.NODE_ENV != 'development') {
//     axios.defaults.baseURL = '/app/index.php?c=entry&i=1&m=tb_toupiao&wxref=mp.weixin.qq.com&do='
// }else{
//     axios.defaults.baseURL = '/app/index.php?c=entry&i=1&m=tb_toupiao&wxref=mp.weixin.qq.com&do='
// }
// axios.defaults.baseURL = 'http://cstec.mcnvp.com/app/index.php?c=entry&i=10&do=api&m=cnvp_same_root_sign&r='

axios.defaults.timeout = 10000

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'

axios.defaults.withCredentials = true;
// http请求拦截器
axios.interceptors.request.use(
    config => {
        if (localStorage.getItem('token')) {
            config.headers = {
                token: localStorage.getItem('token')
            }
        }
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

// http响应拦截器
axios.interceptors.response.use(
    response => {
        return response.data
    },
    error => {
        console.log(error)
        if (error.response) {
            switch (error.response.status) {
                case 401:
                    // 返回 401 清除token信息并跳转到登录页面
                    localStorage.removeItem('token')
                    router.replace({
                        path: '/login',
                        query: {
                            redirect: router.currentRoute.fullPath
                        }
                    })
            }
        }
        return Promise.reject(error)
    }
)

export default axios